package stz.flash.option;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import stz.flash.string.OptionInformation;
import stz.flash.string.OptionString;
import blackberry.util.field.DjChoiceField;
import blackberry.util.field.DjLabelField;


public class TimerDurationChoiceField extends DjChoiceField implements FieldChangeListener {

	private static final String STRING_OF_TEST = "Test";
	private static final String STRING_OF_1_MINUTE = "1분";
	private static final String STRING_OF_2_MINUTE = "2분";
	private static final String STRING_OF_3_MINUTE = "3분";
	private static final String STRING_OF_4_MINUTE = "4분";
	private static final String STRING_OF_5_MINUTE = "5분";
	private static final String STRING_OF_6_MINUTE = "6분";
	private static final String STRING_OF_7_MINUTE = "7분";
	private static final String STRING_OF_8_MINUTE = "8분";
	private static final String STRING_OF_9_MINUTE = "9분";
	private static final String STRING_OF_10_MINUTE = "10분";
	private static final String STRING_OF_UNLIMITED = "무제한";
	
	public static final Integer INTEGER_OF_TEST = new Integer(5);
	public static final Integer INTEGER_OF_1_MINUTE = new Integer(60);
	public static final Integer INTEGER_OF_2_MINUTE = new Integer(120);
	public static final Integer INTEGER_OF_3_MINUTE = new Integer(180);
	public static final Integer INTEGER_OF_4_MINUTE = new Integer(240);
	public static final Integer INTEGER_OF_5_MINUTE = new Integer(300);
	public static final Integer INTEGER_OF_6_MINUTE = new Integer(360);
	public static final Integer INTEGER_OF_7_MINUTE = new Integer(420);
	public static final Integer INTEGER_OF_8_MINUTE = new Integer(480);
	public static final Integer INTEGER_OF_9_MINUTE = new Integer(540);
	public static final Integer INTEGER_OF_10_MINUTE = new Integer(600);
	public static final Integer INTEGER_OF_INTEGER_MAX_VALUE = new Integer(Integer.MAX_VALUE);
	
	private TimerDurationController timerController;
	
	private static final TimerDurationChoiceField instance = new TimerDurationChoiceField();

	private TimerDurationChoiceField() {
		super(DjLabelField.createSimpleLabel(OptionString.TIMER_DURATION_CHOICE_FIELD),
				OptionLabel.createInfoLabel(OptionInformation.TIMER_DURATION_CHOICE_FIELD),
				new String[] {STRING_OF_TEST,
						STRING_OF_1_MINUTE,
						STRING_OF_2_MINUTE,
						STRING_OF_3_MINUTE,
						STRING_OF_4_MINUTE,
						STRING_OF_5_MINUTE,
						STRING_OF_6_MINUTE,
						STRING_OF_7_MINUTE,
						STRING_OF_8_MINUTE,
						STRING_OF_9_MINUTE,
						STRING_OF_10_MINUTE,
						STRING_OF_UNLIMITED});
		
		timerController = TimerDurationController.getIns();
		updateSelected();
		setChangeListener(this);
	}
	
	public static TimerDurationChoiceField getIns(){
		return instance;
	}
	
	public void fieldChanged(Field field, int i) {
		if (i == PROGRAMMATIC || i == 0)
			return;
		setDurationCurrentSelect();
	}
	
	public void updateSelected(){
		setSelectedIndex(getStringOfDuration());
	}
	
	public String getStringOfDuration() {

		if (timerController.getTimerDuration().equals(INTEGER_OF_1_MINUTE))
			return STRING_OF_1_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_2_MINUTE))
			return STRING_OF_2_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_3_MINUTE))
			return STRING_OF_3_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_4_MINUTE))
			return STRING_OF_4_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_5_MINUTE))
			return STRING_OF_5_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_6_MINUTE))
			return STRING_OF_6_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_7_MINUTE))
			return STRING_OF_7_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_8_MINUTE))
			return STRING_OF_8_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_9_MINUTE))
			return STRING_OF_9_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_10_MINUTE))
			return STRING_OF_10_MINUTE;
		
		else if (timerController.getTimerDuration().equals(INTEGER_OF_INTEGER_MAX_VALUE))
			return STRING_OF_UNLIMITED;
		
		//test
		if (timerController.getTimerDuration().equals(INTEGER_OF_TEST)) {
			return STRING_OF_TEST;
		}
		
		return STRING_OF_UNLIMITED;
	}

	public void setDurationCurrentSelect() {
		
		if (getChoice(getSelectedIndex()).equals(STRING_OF_1_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_1_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_2_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_2_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_3_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_3_MINUTE);

		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_4_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_4_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_5_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_5_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_6_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_6_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_7_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_7_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_8_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_8_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_9_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_9_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_10_MINUTE)) {
			timerController.setTimerDuration(INTEGER_OF_10_MINUTE);
			
		} else if (getChoice(getSelectedIndex()).equals(STRING_OF_UNLIMITED)) {
			timerController.setTimerDuration(INTEGER_OF_INTEGER_MAX_VALUE);
		}
		
		//test
		if (getChoice(getSelectedIndex()).equals(STRING_OF_TEST)) {
			timerController.setTimerDuration(INTEGER_OF_TEST);
		}
	}
}